/**
 * 庭审点播
 */
var caseid=$("#caseid").val();
$(function(){
	//浏览器不支持indexOf方法时添加indexOf
	addIndeOf();
	//获取庭审内容
//	$.ajax({
//		type:'get',
//		url:$Path+'/admin/getLiveContent',
//		data:{caseid:caseid},
//		success:function(data){
//			if(data.code==200){
//				var content=data.results;
//				ue.options.initialContent=content;
//			}
//		}
//	})
//	
	tmpinterVal=setInterval(function(){
		if(ue.body!=undefined){
			window.clearInterval(tmpinterVal);
			$.ajax({
				type:'get',
				url:$Path+'/admin/getLiveContent',
				data:{caseid:caseid},
				success:function(data){
					if(data.code==200){
						var content=data.results;
						ue.setContent(content);
					}
				}
			})
		}
	},1000);
	
	
	//添加编辑器监听事件，选区发生修改
	ue.addListener('selectionchange',function(){
				var mynone=ue.selection.getRange()
				var start=mynone.startContainer;
				var node=start.nodeType == 3 ? start.parentNode : start;
				//当前节点包含事件的获取当前时间，不包含的获取父节点时间
				if(node.attributes.datastr!=undefined){
					var time=node.attributes.datastr.nodeValue;
					skipMp3(time);
				}
				else{
					var parent=node.parentNode;
					if(parent!=undefined&&parent.attributes.datastr!=undefined){
						var time=parent.attributes.datastr.nodeValue;
						skipMp3(time);
					}
				}
	})
})
//浏览器不支持indexOf方法时添加indexOf
function addIndeOf(){
	if (!Array.prototype.indexOf){
		Array.prototype.indexOf = function(elt /*, from*/){
	    var len = this.length >>> 0;

	    var from = Number(arguments[1]) || 0;
	    from = (from < 0)
	         ? Math.ceil(from)
	         : Math.floor(from);
	    if (from < 0)
	      from += len;

	    for (; from < len; from++){
	      if (from in this && this[from] === elt)
	        return from;
	    }
	    return -1;
	  };
	}
}
/**
 * 跳转到指定时间
 * @param time
 */
function skipMp3(time){
	if(time!=null&&time!=undefined){
		time=parseInt(time/1000);
		$("#jplayer").jPlayer("play",time);
	}
}
/**
 * 添加词汇
 */
function addWord(){
	var context = $("#content").val();
	var caseid = $("#caseid").val();
	var auth=window.parent.getAuth();
	$.ajax({
		type:"post",
		data:{'drWord.context':context,'drWord.caseid':caseid,authenticityToken:auth},
		url:$Path+'/admin/caseWord/addCaseWord',
		success:function(data){
			if(data.code==200){
				$("#content").val('')
				refreshWord();
			}
			else{
				parent.$.messager.alert('失败',data.msg,'error');
			}
		},
		error:function(data){
			parent.$.messager.alert('失败',"网络错误",'error');
		}
	})
	
	
}
/**
 * 刷新关键词
 */
function refreshWord(){
	var caseid = $("#caseid").val();
	$.ajax({
		type:"get",
		data:{caseid:caseid},
		url:$Path+'/admin/caseWord/findCaseWordByCaseId',
		success:function(data){
			var html = "";
			for(var i = 0;i<data.results.length;i++){
				html+='<li class="titleli ">'+
						'<span class="titlespan" ondblclick="changeContent(\''+data.results[i].context+'\',\''+data.results[i].casewordid+'\')">'+data.results[i].context+'</span>'+
						'<img src="'+$Path+'/public/images/icon_delete.png" class="titleimg" onclick="delkeyword(\''+data.results[i].casewordid+'\')">'+
					'</li>';
			}
			$(".shaixuan").html(html);
		},
		error:function(data){
			parent.$.messager.alert('失败',"网络错误",'error');
		}
	})
}
/**
 * 删除关键词
 * @param casewordid
 */
function delkeyword(casewordid){
	parent.$.messager.confirm('删除','您确定要删除该词条吗？',function(data){
		if(data){
			var auth=window.parent.getAuth();
			$.ajax({
				type:"post",
				data:{caseWordid:casewordid,authenticityToken:auth},
				url:$Path+'/admin/caseWord/deleteCaseWordById',
				success:function(data){
					if(data.code==200){
						refreshWord();
					}
					else{
						parent.$.messager.alert('删除失败',data.msg,'error');
					}
				},
				error:function(data){
					parent.$.messager.alert('删除失败',"网络错误",'error');
				}
			})
		}
	})
}
/**
 * 插入词条
 * @param context
 * @param id
 */
function changeContent(context,id){
	ue.execCommand('inserthtml',context);
}
/**
 * 打印庭审笔录
 */
function printCourt(){
	ue.execCommand("print");
}
/**
 * 到处庭审笔录
 */
function exportCourt(){
	var content = ue.getAllHtml();
	$("#exportHtml").val(content);
	$("#authenticityToken").val(window.parent.getAuth());
	$("#exportCourt").submit();
}
/**
 * 保存庭审笔录
 */
function saveCourt(){
	var content = ue.getContent();
	var caseid = $("#caseid").val();
	var auth=window.parent.getAuth();
	$.ajax({
		type:"post",
		data:{"drCaseContent.caseid":caseid,"drCaseContent.content":content,authenticityToken:auth},
		url:$Path+'/admin/saveCaseContent',
		success:function(data){
			if(data.code!=200){
				parent.$.messager.alert('保存笔录失败',data.msg,'error');
			}
		},
		error:function(data){
			parent.$.messager.alert('保存失败',"网络错误",'error');
		}
	})
}

